package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.FixReportDao;
import com.student.db.FixReportEntity;
import com.student.db.PushEntity;
import com.student.db.ScoreDao;
import com.student.db.ScoreEntity;
import com.student.dms.databinding.ActivityAnnouncementPushBinding;
import com.student.dms.databinding.ActivityDayScoreBinding;
import com.student.dms.databinding.ActivityFixHistoryBinding;
import com.student.dms.databinding.ItemPushBinding;
import com.student.dms.databinding.ItemRecyerviewBinding;
import com.student.dms.databinding.ItemScoreBinding;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

//日常评分
public class ActivityDayScore extends BaseActivity {

    private ActivityDayScoreBinding activityDayScoreBinding;
    private BaseQuickAdapter<ScoreEntity, VH> baseQuickAdapter;
    private AppDatabase roomDatabase;
    private FixReportDao fixReportDao;
    private AccountDao accountDao;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        ScoreDao scoreDao = roomDatabase.provideScoreDao();
        activityDayScoreBinding = ActivityDayScoreBinding.inflate(getLayoutInflater());
        setContentView(activityDayScoreBinding.getRoot());
        activityDayScoreBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });

        activityDayScoreBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ActivityDayScore.this, ActivityEditDayScore.class));
            }
        });
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");

        baseQuickAdapter = new BaseQuickAdapter<ScoreEntity, VH>() {
            @NonNull
            @Override
            protected VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                VH vh = new VH(ItemScoreBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull VH vh, int position, @Nullable ScoreEntity itemData) {
                vh.itemViewBidnng.roomNo.setText("寝室号："+itemData.getRoomNo());
                vh.itemViewBidnng.bedScore.setText("床："+itemData.getBedScore()+"分");
                vh.itemViewBidnng.floorScore.setText("地面："+itemData.getFloorScore()+"分");
                vh.itemViewBidnng.desktopScore.setText("桌面："+itemData.getDesktopScore()+"分");
                vh.itemViewBidnng.balconyScore.setText("阳台："+itemData.getBalconyScore()+"分");
                vh.itemViewBidnng.scoreTime.setText("评分时间："+itemData.getScoreTime());
            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        activityDayScoreBinding.recyclerView.setAdapter(build.getAdapter());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityDayScore.this);
        activityDayScoreBinding.recyclerView.setLayoutManager(linearLayoutManager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(ActivityDayScore.this, DividerItemDecoration.VERTICAL);
        activityDayScoreBinding.recyclerView.addItemDecoration(dividerItemDecoration);

        if (!TextUtils.isEmpty(username)) {
            AccountDao accountDao = roomDatabase.provideAccountDao();
            Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
            disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                if (accountEntity.getType() == 1) {
                    activityDayScoreBinding.add.setVisibility(View.VISIBLE);
                } else {
                    activityDayScoreBinding.add.setVisibility(View.GONE);
                }

                disposable.add(scoreDao.getScores().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(list -> {
                    List<ScoreEntity> collect = list.stream().filter(scoreEntity -> {
                        return TextUtils.equals(scoreEntity.getRoomNo(), accountEntity.getRoomNo());
                    }).collect(Collectors.toList());
                    baseQuickAdapter.submitList(collect);
                }));
            }, studentFlowThrowable -> {
                ToastUtils.showToast(ActivityDayScore.this, "用户不存在，请注册！");
            }));
        }


    }

    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemScoreBinding itemViewBidnng;

        public VH(@NonNull ItemScoreBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }

}
